package com.ceair.flight.refund.service.impl;


import com.ceair.flight.refund.location.dao.IEmdOrderDao;
import com.ceair.flight.refund.location.dao.IFlightOrderDao;
import com.ceair.flight.refund.location.dao.IOrderDao;
import com.ceair.flight.refund.location.dao.IWifiOrderDao;
import com.ceair.flight.refund.location.model.EmdOrderDO;
import com.ceair.flight.refund.location.model.FlightOrderDO;
import com.ceair.flight.refund.location.model.OrderDO;
import com.ceair.flight.refund.location.model.WifiOrderDO;
import com.ceair.flight.refund.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class OrderServiceImpl implements IOrderService {

    private static final Logger LOG = LoggerFactory.getLogger(OrderServiceImpl.class);
    @Autowired
    private IOrderDao orderDao;
    @Autowired
    private IFlightOrderDao flightOrderDao;
    @Autowired
    private IEmdOrderDao emdOrderDao;
    @Autowired
    private IWifiOrderDao wifiOrderDao;

    @Override
    public OrderDO queryOrderDetail(String orderNo) {
        if (StringUtils.isEmpty(orderNo)){
            return null;
        }
        try {
            OrderDO order = orderDao.queryOrder(orderNo);
            //机票
            List<FlightOrderDO> flightOrderDOs = flightOrderDao.getFlightOrderListByOrderNo(orderNo);
            order.setFlightOrders(new HashSet(flightOrderDOs));
            //选座
            Set<EmdOrderDO> emdOrderDOs = emdOrderDao.queryEmdOrderByOrderNo(orderNo);
            order.setEmdOrders(emdOrderDOs);
            //wifi
            Set<WifiOrderDO> wifiOrderDOs = wifiOrderDao.getWifiOrderDetailByOrderNo(orderNo);
            order.setWifiOrders(wifiOrderDOs);
            return order;
        } catch (Exception e) {
            LOG.error("-------getOrderDetail error,orderNo: " + orderNo,e);
            return null;
        }
    }

    @Override
    public OrderDO queryFlightOrderDetail(String orderNo) {
        try {
            OrderDO order = orderDao.queryOrder(orderNo);
            if(order == null){
                LOG.error("Order is null,orderNo:{}",orderNo);
                return null;
            }
            //机票
            List<FlightOrderDO> flightOrderDOs = flightOrderDao.getFlightOrderListByOrderNo(orderNo);
            if (CollectionUtils.isEmpty(flightOrderDOs)){
                LOG.error("FlightOrder is null,orderNo:{}",orderNo);
                return null;
            }
            order.setFlightOrders(new HashSet(flightOrderDOs));
            return order;
        }catch (Exception e){
            LOG.error("-------getFlightOrderDetail error,orderNo: " + orderNo,e);
            return null;
        }
    }

    /**
     * 乘客状态
     * paxSt: 1待开票、2已开票、3已退票、4已改期、5已升舱、6已挂起
     * @param orderNo
     * @param paxSt
     * @return
     */
    @Override
    public OrderDO queryOrderDetail(String orderNo, Integer paxSt) {
        if (StringUtils.isEmpty(orderNo)){
            return null;
        }
        try {
            OrderDO order = orderDao.queryOrder(orderNo);
            //机票
            List<FlightOrderDO> flightOrderDOs = flightOrderDao.getFlightOrderDetail(orderNo,paxSt);
            order.setFlightOrders(new HashSet(flightOrderDOs));
            //选座
            Set<EmdOrderDO> emdOrderDOs = emdOrderDao.queryEmdOrderByOrderNo(orderNo);
            order.setEmdOrders(emdOrderDOs);
            //wifi
            Set<WifiOrderDO> wifiOrderDOs = wifiOrderDao.getWifiOrderDetailByOrderNo(orderNo);
            order.setWifiOrders(wifiOrderDOs);
            return order;
        } catch (Exception e) {
            LOG.error("-------getOrderDetail error,orderNo: " + orderNo,e);
            return null;
        }
    }

    /**
     * 乘客状态
     * paxSt: 1待开票、2已开票、3已退票、4已改期、5已升舱、6已挂起
     * @param orderNo
     * @param paxSt
     * @return
     */
    @Override
    public OrderDO queryFlightOrderDetail(String orderNo, Integer paxSt) {
        try {
            OrderDO order = orderDao.queryOrder(orderNo);
            if(order == null){
                LOG.error("Order is null,orderNo:{}",orderNo);
                return null;
            }
            //机票
            List<FlightOrderDO> flightOrderDOs = flightOrderDao.getFlightOrderDetail(orderNo,paxSt);
            if (CollectionUtils.isEmpty(flightOrderDOs)){
                LOG.error("FlightOrder is null,orderNo:{}",orderNo);
                return null;
            }
            order.setFlightOrders(new HashSet(flightOrderDOs));
            return order;
        }catch (Exception e){
            LOG.error("-------getFlightOrderDetail error,orderNo: " + orderNo,e);
            return null;
        }
    }
}
